//
//  HJZStatusTopView.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/6/1.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "HJZStatusTopView.h"
#import "HJZPhotosView.h"
#import "HJZRetweetStatusView.h"
#import <UIImageView+WebCache.h>
#import "HJZUser.h"
#import "RCLabel.h"
#import "HtmlString.h"
#import "UIImage+Extend.h"
@interface HJZStatusTopView ()<RCLabelDelegate, UIGestureRecognizerDelegate>
/** 原创微博头像 */
@property (nonatomic, weak) UIImageView *iconView;
/** 原创微博昵称 */
@property (nonatomic, weak) UIButton *nameBtn;
/** 原创微博vip */
@property (nonatomic, weak) UIImageView *vipView;
/** 删除微博按钮*/
@property (nonatomic, weak) UIButton *deleteBtn;
/** 原创微博时间 */
@property (nonatomic, weak) UILabel *timeLabel;
/** 原创微博来源 */
@property (nonatomic, weak) UILabel *sourceLabel;
/** 原创微博正文 */
@property (nonatomic, weak) RCLabel *contentLabel;
/** 原创微博配图*/
@property (nonatomic, weak) HJZPhotosView *photosView;








@end

@implementation HJZStatusTopView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.image = [UIImage resizedImageWithName:@"timeline_card_top_background"];
        self.highlightedImage = [UIImage resizedImageWithName: @"timeline_card_top_background_highlighted"];
        
        // 1. 头像
        UIImageView *iconView = [[UIImageView alloc] init];
        [self addSubview:iconView];
        self.iconView = iconView;
        
        // 2. 昵称
        UIButton *nameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        nameBtn.titleLabel.font = [UIFont systemFontOfSize:HJZStatusNameFont];
        [self addSubview:nameBtn];
        [nameBtn addTarget:self action:@selector(nameBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        self.nameBtn = nameBtn;
        
        // 3. 会员图标
        UIImageView *vipView = [[UIImageView alloc] init];
        vipView.contentMode = UIViewContentModeCenter;
        [self addSubview:vipView];
        self.vipView = vipView;
        
        // 删除按钮
        UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [deleteBtn setImage:[UIImage resizedImageWithName:@"timeline_icon_more"] forState:UIControlStateNormal];
        [deleteBtn setImage:[UIImage resizedImageWithName:@"timeline_icon_more_highlighted"] forState:UIControlStateHighlighted];
        [deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:deleteBtn];
        self.deleteBtn = deleteBtn;
        
        
        // 4. 时间
        UILabel *timeLabel = [[UILabel alloc] init];
        timeLabel.font = [UIFont systemFontOfSize:HJZStatusTimeFont];
        timeLabel.textColor = COLOR(135, 135, 135, 1);
        [self addSubview:timeLabel];
        self.timeLabel = timeLabel;
        
        // 5. 来源
        UILabel *sourceLabel = [[UILabel alloc] init];
        sourceLabel.font = [UIFont systemFontOfSize:HJZStatusSourceFont];
        sourceLabel.textColor = COLOR(333, 135, 135, 1);
        
        [self addSubview:sourceLabel];
        self.sourceLabel = sourceLabel;
        
        // 6. 正文
        RCLabel *contentLabel = [[RCLabel alloc] init];
        contentLabel.delegate = self;
        contentLabel.font = [UIFont systemFontOfSize:HJZStatusContentFont];
        contentLabel.textColor = COLOR(35, 35, 35, 1);
        contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
        [self addSubview:contentLabel];
        self.contentLabel = contentLabel;
        
        // 7. 配图
        HJZPhotosView *photosView = [[HJZPhotosView alloc] init];
        [self addSubview:photosView];
        self.photosView = photosView;
        
        // 8. 初始化转发微博
        [self setupRetweetStatus];
    }
    return self;
}

/**
 *  初始化转发微博
 */
- (void)setupRetweetStatus
{
    HJZRetweetStatusView *retweetView = [[HJZRetweetStatusView alloc] init];
    [self addSubview:retweetView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    tap.delegate = self;
    [retweetView addGestureRecognizer:tap];
    self.retweetView = retweetView;
}

#pragma mark - 设置数据

- (void)setStatusFrame:(HJZStatusFrame *)statusFrame
{
    _statusFrame = statusFrame;
    HJZStatus *status = statusFrame.status;
    HJZUser *user = status.user;
    // 头像
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:user.profile_image_url]
                     placeholderImage:[UIImage imageNamed:@"avatar_default_small"]];
    self.iconView.frame = statusFrame.iconViewF;
    
    // 昵称
    [self.nameBtn setTitle:status.user.name forState:UIControlStateNormal];
    self.nameBtn.frame = statusFrame.nameBtnF;
    
    // vip
    if (status.user.mbtype > 0) {
        self.vipView.hidden = NO;
        NSString *name = [NSString stringWithFormat:@"common_icon_membership_level%d", user.mbrank];
        self.vipView.image = [UIImage imageNamed:name];
        self.vipView.frame = statusFrame.vipViewF;
        
        [self.nameBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    } else {
        self.vipView.hidden = YES;
        
        [self.nameBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    // 删除按钮
    self.deleteBtn.frame = CGRectMake(statusFrame.originalViewF.size.width - 30, _nameBtn.frame.origin.y, 30, 30);
    
    // 时间
    self.timeLabel.text = status.created_at;
    self.timeLabel.frame = statusFrame.timeLabelF;
   
    // 来源
    self.sourceLabel.text = status.source;
    self.sourceLabel.frame = statusFrame.sourceLabelF;
    
    // 正文
    NSString *str = [HtmlString transformString:status.text];
    self.contentLabel.componentsAndPlainText = [RCLabel extractTextStyle:str];
    self.contentLabel.frame = statusFrame.contentLabelF;
    
    // 配图
    if (status.pic_urls.count) {
        self.photosView.hidden = NO;
        
        self.photosView.photos = status.pic_urls;
        
        self.photosView.frame = statusFrame.photoViewF;
    } else {
        self.photosView.hidden = YES;
    }
    // 设置转发微博数据和frame
    [self setupRetweetData];

    
}

/**
 *  设置转发微博数据和frame
 */
- (void)setupRetweetData
{
    HJZStatusFrame *statusFrame = self.statusFrame;
    HJZStatus *status = statusFrame.status;
    
    // 转发微博
    if (status.retweeted_status) {
        self.retweetView.hidden = NO;
        self.retweetView.frame = statusFrame.retweetViewF;
        
        // 传递模型数据
        self.retweetView.statusFrame = statusFrame;
    } else {
        self.retweetView.hidden = YES;
    }
}
/**
 *  轻拍方法
 */
- (void)tapAction:(UITapGestureRecognizer *)tap {
    if ([self.delegate respondsToSelector:@selector(statusTopViewTapWithRetweenStatus:)]) {
        [self.delegate statusTopViewTapWithRetweenStatus:_statusFrame.status.retweeted_status];
    }
    
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    if ([touch.view isMemberOfClass:[RCLabel class]]){
        
        return NO;
        
    }
    
    return YES;
    
}
#pragma mark --- 点击删除微博按钮
- (void)deleteBtnAction:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(statusTopViewDeleteWithStatus:WithCell:)]) {
        UITableViewCell *cell = (UITableViewCell *)btn.superview.superview.superview;
        [self.delegate statusTopViewDeleteWithStatus:_statusFrame.status WithCell:(UITableViewCell *)cell];
    }
}

#pragma mark --- RCLabel代理方法 ---
- (void)RCLabel:(id)RCLabel didSelectLinkWithURL:(NSString *)url {
    if ([self.delegate respondsToSelector:@selector(statusTopViewClickWithUrl:)]) {
        [self.delegate statusTopViewClickWithUrl:url];
    }

}
#pragma mark -点击昵称
- (void)nameBtnAction:(UIButton *)btn {
    
    if ([self.delegate respondsToSelector:@selector(statusTopViewClickWithName:)]) {
        [self.delegate statusTopViewClickWithName:_statusFrame.status.user.name];
    }
}

@end
